package ESP.Control.B;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import ESP.Dao.BaseDao;
import ESP.Util.IDFactory;
import ESP.Util.UploadConfigurationRead;
import ESP.VO.Data;
import ESP.VO.Role;

import com.alibaba.fastjson.JSONArray;

/**
 * 
 * @author 程尧
 * @version Report
 *
 */
public class b01 {
	
	@Resource
	public BaseDao baseDao;
	/**
	 * 返回信息
	 */
	private JSONArray rows;
	private Map<String, String> message = new HashMap<String, String>();
	/**
	 * 用户信息
	 */
	private String user_role_id;
	private String user_id;
	/**
	 * 表头查询
	 * @return
	 */
	private String stuId;
	private String userName;
	private Date dataCretime1;
	private Date dataCretime2;
	private String dataReportFlag;
	private String typeId;
	/**
	 * 表体查询
	 */
	private String stu_id;
	/**
	 * file
	 */
	private File[] upload;// 实际上传文件
    private String[] uploadContentType; // 文件的内容类型
    private String[] uploadFileName; // 上传文件名
    private List<Data> uploadFiles = new ArrayList<Data>();
    /**
     * 下载地址
     */
    private String downloadPath;
    /**
     * DATA
     */
    private String data_id;
    private String data_name;
	private String data_type_id;//
	private Date data_cretime;
	private String data_path;
	private String data_owner_id;
	private String data_note;
	private String data_flag;//数据标志1\2\3
	private String data_report_flag;
	
	/**
	 * 查询表头
	 * @return
	 */
	public String queryReportHead(){
		if (!user_role_id.equals(Role.ADMIN) && !user_role_id.equals(Role.TEACHER)) {//如果是学生
			this.stuId = this.user_id;
		}
		List list = baseDao.queryDataHeadB01(user_role_id, stuId, userName, dataCretime1, dataCretime2, dataReportFlag, typeId);
		if (list.size() == 0) {
			baseDao.setReturnInfo(message, "N", "查询完毕,没有符合条件的数据!");
		}else {
			baseDao.setReturnInfo(message, "Y", "查询完毕,共"+list.size()+"条数据!");
		}
		this.rows = baseDao.toJson(list);
		return "queryReportHead";
	}
	/**
	 * 查询表体queryReportBody
	 */
	public String queryReportBody(){
		List list;
		list = baseDao.queryDataBodyB01(this.stu_id);
		if (list.size() == 0) {
			baseDao.setReturnInfo(message, "N", "暂未上传支撑文档!");
		}
		this.rows = baseDao.toJson(list);
		return "queryReportBody";
	}
	/**
	 * 上传Report
	 * @return
	 */
	public String uploadReport() {
		try {
			String relativePath = 
				"/" + UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim();
			String targetDirectory = ServletActionContext.getServletContext().getRealPath(relativePath);// 获得路径
			for (int i = 0; i < upload.length; i++) {
				String fileName = uploadFileName[i];// 上传的文件名
				String type = UploadConfigurationRead.getExt(fileName);// 文件类型
				String realName = IDFactory.getID();// 保存的文件名称
				
				// 在把路径写入数据库即可
				this.data_id = realName;
				this.data_name = fileName;
				this.data_type_id = type;
				this.data_path = relativePath+"/"+realName+"."+type;
				this.data_owner_id = this.user_id;
				this.data_flag = Data.STUDENT_REPORT;
				if (data_report_flag == null || data_report_flag.equals("")) {
					data_report_flag = "N";
				}
				System.out.println("data_type_id:"+data_type_id);
				System.out.println("data_path:"+data_path);
				baseDao.addData(data_id, data_name, data_type_id, data_cretime, data_path, data_owner_id,
						data_note, data_flag, data_report_flag);
				File target = new File(targetDirectory, realName+"."+type);
				FileUtils.copyFile(upload[i], target);// 上传至服务器的目录，一般都这样操作
				baseDao.setReturnInfo(message, "Y", fileName+"上传成功!");
				System.out.println("Report上传成功");
			}
		} catch (Exception e) {
			baseDao.setReturnInfo(message, "N", "服务器忙!Report上传失败,请稍后重试!");
			System.out.println("上传Report失败");
			e.printStackTrace();
			return "uploadReport";
		}
		return "uploadReport";
	}
	/**
	 * 上传支撑文档
	 */
	public String uploadSupport() {
		try {
			String relativePath = 
				"/" + UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim();
			String targetDirectory = ServletActionContext.getServletContext().getRealPath(relativePath);// 获得路径
			for (int i = 0; i < upload.length; i++) {
				String fileName = uploadFileName[i];// 上传的文件名
				String type = UploadConfigurationRead.getExt(fileName);// 文件类型
				String realName = IDFactory.getID();// 保存的文件名称
				
				// 在把路径写入数据库即可
				this.data_id = realName;
				this.data_name = fileName;
				this.data_type_id = type;
				this.data_path = relativePath+"/"+realName+"."+type;
				this.data_owner_id = this.user_id;
				this.data_flag = Data.STUDENT_DATA;
				this.data_report_flag = Data.NOT_REPORT;
				
				baseDao.addData(data_id, data_name, data_type_id, data_cretime, data_path, data_owner_id,
						data_note, data_flag, data_report_flag);
				File target = new File(targetDirectory, realName+"."+type);
				FileUtils.copyFile(upload[i], target);// 上传至服务器的目录，一般都这样操作
				baseDao.setReturnInfo(message, "Y", fileName+"上传成功!");
				System.out.println("支撑文档上传成功");
			}
		} catch (Exception e) {
			baseDao.setReturnInfo(message, "N", "文档上传失败,请稍后重试!");
			System.out.println("上传支撑文档失败");
			e.printStackTrace();
			return "uploadSupport";
		}
		return "uploadSupport";
	}
	/**
	 * 删除report文件
	 * @return
	 */
	public String removeReport(){
		int i = baseDao.removeData(this.data_id);
		if (i == 0) {
			baseDao.setReturnInfo(message, "N", "删除失败!");
			return "removeReport";
		}
		baseDao.setReturnInfo(message, "Y", "删除成功!");
		System.out.println("删除data成功");
		return "removeReport";
	}
	/**
	 * 查询文件类型
	 * @return
	 */
	public String queryDataType(){
		List list = baseDao.querySqlForList("select type_id,type_name from esp_datatype ORDER BY type_name");
		this.rows = baseDao.toJson(list);
		return "queryDataType";
	}
	//===========set,get
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public JSONArray getRows() {
		return rows;
	}

	public void setRows(JSONArray rows) {
		this.rows = rows;
	}

	public String getUser_role_id() {
		return user_role_id;
	}

	public void setUser_role_id(String userRoleId) {
		user_role_id = userRoleId;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String userId) {
		user_id = userId;
	}

	public String getStuId() {
		return stuId;
	}

	public void setStuId(String stuId) {
		this.stuId = stuId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Date getDataCretime1() {
		return dataCretime1;
	}

	public void setDataCretime1(Date dataCretime1) {
		this.dataCretime1 = dataCretime1;
	}

	public Date getDataCretime2() {
		return dataCretime2;
	}

	public void setDataCretime2(Date dataCretime2) {
		this.dataCretime2 = dataCretime2;
	}

	public String getDataReportFlag() {
		return dataReportFlag;
	}

	public void setDataReportFlag(String dataReportFlag) {
		this.dataReportFlag = dataReportFlag;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getData_id() {
		return data_id;
	}
	public void setData_id(String dataId) {
		data_id = dataId;
	}
	public String getData_type_id() {
		return data_type_id;
	}
	public void setData_type_id(String dataTypeId) {
		data_type_id = dataTypeId;
	}
	public Date getData_cretime() {
		return data_cretime;
	}
	public void setData_cretime(Date dataCretime) {
		data_cretime = dataCretime;
	}
	public String getData_path() {
		return data_path;
	}
	public void setData_path(String dataPath) {
		data_path = dataPath;
	}
	public String getData_owner_id() {
		return data_owner_id;
	}
	public void setData_owner_id(String dataOwnerId) {
		data_owner_id = dataOwnerId;
	}
	public String getData_note() {
		return data_note;
	}
	public void setData_note(String dataNote) {
		data_note = dataNote;
	}
	public String getData_flag() {
		return data_flag;
	}
	public void setData_flag(String dataFlag) {
		data_flag = dataFlag;
	}
	public String getData_report_flag() {
		return data_report_flag;
	}
	public void setData_report_flag(String dataReportFlag) {
		data_report_flag = dataReportFlag;
	}
	public String getData_name() {
		return data_name;
	}
	public void setData_name(String dataName) {
		data_name = dataName;
	}
	public String getDownloadPath() {
		return downloadPath;
	}
	public void setDownloadPath(String downloadPath) {
		this.downloadPath = downloadPath;
	}
	public String getStu_id() {
		return stu_id;
	}
	public void setStu_id(String stuId) {
		stu_id = stuId;
	}
	public Map<String, String> getMessage() {
		return message;
	}
	public void setMessage(Map<String, String> message) {
		this.message = message;
	}
}
